package com.system.controller;

import com.system.entity.*;
import com.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
/**
 * 学生功能模块
 */
import java.util.*;
@CrossOrigin
@RequestMapping("/")
@Controller
public class StudentController {
    @Autowired
    private courseService courseService;
    @Autowired
    private takeCourseService takeCourseService;
    @Autowired
    private courseInformationService courseInformationService;
    @Autowired
    private teacherService teacherService;
    @Autowired
    private com.system.service.teachCourseService teachCourseService;

    /**
     * 学生分数查询
     * @param map
     * @return
     */
    @RequestMapping("studentgrade")
    @ResponseBody
    public  List<Grade> result_grade(@RequestBody Map<String,String> map){
        Long stu_id = Long.valueOf(map.get("user_id"));  //提取前端传过来的学生学号
        //Long stu_id = 201712110104L;
        String semester = map.get("semester"); //提取前端传过来的学生季度
        Integer course_id = 0;
        if(!map.get("course_id").equals(""))course_id = Integer.valueOf(map.get("course_id"));
        String courseType = map.get("courseType");
        String courseName = map.get("courseName");
        List<Grade> course;
        if (isSemester(semester)&&isCourseID(course_id)){
            course = takeCourseService.findBySemId(stu_id,semester,course_id);
            for(Grade g:course){
                g.setCourse_Type(g.getCourse_character());        }

        }
        else if (isSemester(semester)&&isCourseType(courseType)){
            course = takeCourseService.findBySemType(stu_id,semester,courseType);
            for(Grade g:course){
                g.setCourse_Type(g.getCourse_character());        }
        }
        else if (isSemester(semester)&&isCourseName(courseName)){
            course = takeCourseService.findBySemName(stu_id,semester,courseName);
            for(Grade g:course){
                g.setCourse_Type(g.getCourse_character());        }
        }
        else if (isCourseName(courseName)){
            course = takeCourseService.findByCourseName(stu_id,courseName);
            for(Grade g:course){
                g.setCourse_Type(g.getCourse_character());        }
        }
        else if (isSemester(semester)){
            course = takeCourseService.findBySemester(stu_id,semester);
            for(Grade g:course){
                g.setCourse_Type(g.getCourse_character());        }
        }
        else if (isCourseType(courseType)){
            course = takeCourseService.findByCourseType(stu_id,courseType);
            for(Grade g:course){
                g.setCourse_Type(g.getCourse_character());        }
        }
        else if (isCourseID(course_id)){
            course = takeCourseService.findByCourseID(stu_id,course_id);
            for(Grade g:course){
                g.setCourse_Type(g.getCourse_character());        }
        }
        else {
            course=  takeCourseService.findByStudentId(stu_id);
            for(Grade g:course){
                g.setCourse_Type(g.getCourse_character());        }
        }
        return course;
    }

    /**
     * 学生课程查询
     * @param map
     * @return
     */
    @RequestMapping("studentcourse")
    @ResponseBody
    public List<courseList> result_course(@RequestBody Map<String,String> map){
        Long stu_id =Long.valueOf(map.get("user_id")) ;
        List<CourseInformation> result = courseInformationService.findByStudentId(stu_id);
        courseList courseList1 = new courseList("1-2节");
        courseList courseList2 = new courseList("3-4节");
        courseList courseList3 = new courseList("5-6节");
        courseList courseList4 = new courseList("7-8节");
        courseList courseList5 = new courseList("9-10节");
        courseList courseList6 = new courseList("11-13节");
        List<courseList> C_list = new ArrayList<>();
        Iterator<CourseInformation> iter = result.iterator();
        CourseInformation tmp;
        while(iter.hasNext()){
            tmp = iter.next();
            Integer course_coding = tmp.getCourseCoding();
            String coursename = courseService.findByCourseCoding(course_coding).getCourse_name();
            Teacher teacher = teacherService.findByTeacherId(teachCourseService.findByCourseId(tmp.getCourseId()).getTeaId());
            String teaname = teacher.getTeaName();
            String weekname = tmp.getDaytime().toString();
            String place = tmp.getCoursePlace().toString();
            String week  = tmp.getWeek();
            if (tmp.getCourseTime().equals("1-2")){
                setWeek(week,courseList1,coursename,teaname,weekname,place);
            }
            else if (tmp.getCourseTime().equals("3-4")){
                setWeek(week,courseList2,coursename,teaname,weekname,place);
            }
            else if (tmp.getCourseTime().equals("5-6")){
                setWeek(week,courseList3,coursename,teaname,weekname,place);
            }
            else if (tmp.getCourseTime().equals("7-8")){
                setWeek(week,courseList4,coursename,teaname,weekname,place);
            }
            else if (tmp.getCourseTime().equals("9-10")){
                setWeek(week,courseList5,coursename,teaname,weekname,place);
            }
            else {
                setWeek(week,courseList6,coursename,teaname,weekname,place);
            }
        }
        C_list.add(courseList1);
        C_list.add(courseList2);
        C_list.add(courseList3);
        C_list.add(courseList4);
        C_list.add(courseList5);
        C_list.add(courseList6);
        return C_list;
    }
    @RequestMapping("displaycourse")
    @ResponseBody
    public List<SelectCourse> stuCS_interface(@RequestBody Map<String , String > map){
        Long stu_id = Long.valueOf(map.get("user_id"));
        List <SelectCourse> list = takeCourseService.selectCourse(stu_id);
        return list;
    }
    @RequestMapping("deletecourse")
    @ResponseBody
    public void  stu_deleteC(@RequestBody Map<String , String > map){
        Long stu_id = Long.valueOf(map.get("user_id"));
        Integer course_id = Integer.valueOf(map.get("course_id"));
        TakecourseKey takecourseKey = new TakecourseKey(course_id,stu_id);
        takeCourseService.deleteByPrimaryKey(takecourseKey);
    }
    @RequestMapping("insertcourse")
    @ResponseBody
    public void  stu_selectC(@RequestBody Map<String , String > map){
        Integer courseId = Integer.valueOf(map.get("course_id"));
        Long stuId = Long.valueOf(map.get("stu_id"));
        String coursetype = map.get("coursetype");
        Takecourse takecourse = new Takecourse(courseId,stuId,0,0,0,
                0,coursetype);
        takeCourseService.insert(takecourse);
    }
    /**
     * 课程设置方法
     * @param week
     * @param courselist
     * @param coursename
     * @param teaname
     * @param weekname
     * @param place
     */
    public void setWeek(String week,courseList courselist,String coursename
    ,String teaname,String weekname,String place){
        if (weekname.equals("1")){
            courselist.setMonday(coursename+"\n"+teaname
                  +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("2")){
            courselist.setTuesday(coursename+"\n"+teaname
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("3")){
            courselist.setWednesday(coursename+"\n"+teaname
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("4")){
            courselist.setThursday(coursename+"\n"+teaname
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("5")){
            courselist.setFriday(coursename+"\n"+teaname
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("6")){
            courselist.setSaturday(coursename+"\n"+teaname
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("7")){
            courselist.setSunday(coursename+"\n"+teaname
                    +"\n"+week+"\n"+place);
        }

    }
    public boolean isSemester(String semester){
        if (semester.equals("")){
            return false;
        }
        return true;
    }
    public boolean isCourseID(Integer course_id){
        if (course_id==0){
            return false;
        }
        return true;
    }
    public boolean isCourseName(String course_name){
        if (course_name.equals("")){
            return false;
        }
        return true;
    }
    public boolean isCourseType(String courseType){
        if (courseType.equals("")){
            return false;
        }
        return true;
    }

}
